using Catalog.Application.Categorys.Interfaces;
using Catalog.Application.Categorys.Queries;
using Catalog.Application.Contracts.DTOs;
using MediatR;

namespace Catalog.Application.Categorys.QueryHandlers;

public class SearchCategoryQueryHandler : IRequestHandler<SearchCategoryQuery, List<CategoryProfileDto>>
{
    private readonly ICategoryReadService _categoryReadService;

    public SearchCategoryQueryHandler(ICategoryReadService categoryReadService)
    {
        _categoryReadService = categoryReadService;
    }

    public async Task<List<CategoryProfileDto>> Handle(SearchCategoryQuery request, CancellationToken cancellationToken)
    {
        var categories = await _categoryReadService.GetListAsync(request.NameFilter, cancellationToken);
        return categories.Select(c => new CategoryProfileDto
        {
            Id = c.Id,
            Name = c.Name,
            Description = c.Description
        }).ToList();
    }
}